//
//  TTShareCenter.m
//  TiantuLogistics
//
//  Created by Isaac on 16/12/28.
//  Copyright © 2016年 tiantu. All rights reserved.
//

#import "TTShareCenter.h"

@implementation TTShareCenter

+ (TTShareCenter *)shareCenter {
    static TTShareCenter *center = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        center = [[TTShareCenter alloc]init];
    });
    return center;
}

- (void)shareData:(NSString *)shareUrl description:(NSString *)descrip title:(NSString *)title  shareType:(UMSocialPlatformType)sharetype CurrenController:(UIViewController *)controller View:(UIView *)view {
    
    
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
    
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    
    [view.layer renderInContext:ctx];
    
    // 这个是要分享图片的样式(自定义的)
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图本地
    shareObject.thumbImage = [UIImage imageNamed:@"icon"];
    
    [shareObject setShareImage:newImage];
    
    // 设置Pinterest参数
    if (sharetype == UMSocialPlatformType_Pinterest) {
        [self setPinterstInfo:messageObject];
    }
    
    // 设置Kakao参数
    if (sharetype == UMSocialPlatformType_KakaoTalk) {
        messageObject.moreInfo = @{@"permission" : @1}; // @1 = KOStoryPermissionPublic
    }
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;

    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:sharetype messageObject:messageObject currentViewController:controller completion:^(id data, NSError *error) {
        if (error) {
                        NSLog(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                //                NSLog(@"response message is %@",resp.message);
                //第三方原始返回的数据
                //                NSLog(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                //                NSLog(@"response data is %@",data);
            }
        }
        [self alertWithError:error];
    }];
    
    
}
- (void)setPinterstInfo:(UMSocialMessageObject *)messageObj
{
    messageObj.moreInfo = @{@"source_url": @"http://www.umeng.com",
                            @"app_name": @"U-Share",
                            @"suggested_board_name": @"UShareProduce",
                            @"description": @"U-Share: best social bridge"};
}

- (void)alertWithError:(NSError *)error
{
    NSString *result = nil;
    if (!error) {
        result =  @"分享成功";
    }
    else{
        if (error) {
            switch (error.code) {
                case UMSocialPlatformErrorType_Unknow:{
                    result =  @"分享失败: 未知错误";

                    break;
                }
                case UMSocialPlatformErrorType_NotSupport:{
                    result =  @"分享失败: 客户端版本不支持";

                    break;
                }
                case UMSocialPlatformErrorType_AuthorizeFailed:{
                    result =  @"分享失败: 授权失败";

                    break;
                }
                case UMSocialPlatformErrorType_ShareFailed:{
                    result =  @"分享失败";

                    break;
                }
                case UMSocialPlatformErrorType_RequestForUserProfileFailed:{
                    result =  @"分享失败: 请求用户信息失败";

                    break;
                }
                case UMSocialPlatformErrorType_ShareDataNil:{
                    result =  @"分享失败: 分享内容为空";

                    break;
                }
                case UMSocialPlatformErrorType_ShareDataTypeIllegal:{
                    result = @"分享失败: 分享内容不支持";
                    break;
                }
                case UMSocialPlatformErrorType_CheckUrlSchemaFail:{
                    result = @"分享失败: schemaurl fail";
                    break;
                }
                case UMSocialPlatformErrorType_NotInstall:{
                    result = @"分享失败: 应用未安装";
                    break;
                }
                case UMSocialPlatformErrorType_Cancel:{
                    result = @"分享失败: 用户取消分享";
                    break;
                }
                case UMSocialPlatformErrorType_NotNetWork:{
                    result = @"分享失败: 网络异常";
                    break;
                }
                case UMSocialPlatformErrorType_SourceError:{
                    result =  @"分享失败: 第三方错误";
                    break;
                }
                    
                default:
                    result = @"分享失败";

                    break;
            }
        }
        else{
            result =  @"分享失败";
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"分享"
                                                    message:result
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}

@end
